package com.headfirst.observer.version2;

import com.headfirst.observer.version1.DisplayElement;

import java.util.Observable;
import java.util.Observer;

/**
 * @author sunhongguang
 * @create 2022-01-18-23:29
 */
public class CurrentConditionDisplay_v2 implements Observer, DisplayElement {
    private float temperature;
    private float humidity;
    private Observable observable;

    public CurrentConditionDisplay_v2(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof WeatherData_v2){
            WeatherData_v2 weatherDataV2 = (WeatherData_v2) o;
            this.temperature = weatherDataV2.getTemperature();
            this.humidity = weatherDataV2.getHumidity();
            display();
        }

    }

    @Override
    public void display() {
        System.out.println("Current conditions: "+temperature+"F degrees and "+humidity+"% humidity");
    }
}
